﻿using System;
using Coolite.Ext.Web;
using Pap.Web.Modules.Reports.ViewModel;
using Pap.Web.Utils;

namespace Pap.Web.Modules.Reports.Views
{
    /// <summary>
    /// 
    /// </summary>
    public partial class AccountantMonthlyReport : BasePage<AccountantMonthlyReportViewModel>
    {
        /// <summary>
        /// Handles the Load event of the Page control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void Page_Load(object sender, EventArgs e)
        {
            this.Master.GenerateReport += Master_GenerateReport;
            if (!Ext.IsAjaxRequest)
            {
                InitParams();
            }
        }

        protected void Master_GenerateReport(object sender, EventArgs e)
        {
            var committeeId = cmbCommittees.SelectedItem.Value.ToGuid();
            var month = int.Parse(cmbSSIDMonth.SelectedItem.Value);
            var year = int.Parse(txtYear.Text);

            this.strReport.DataSource = ViewModel.GetAccountantMonthlyData(committeeId, month, year);
            this.strReport.DataBind();
        }

        /// <summary>
        /// Inits the params.
        /// </summary>
        private void InitParams()
        {
            FillCommittees();
            txtYear.Text = DateTime.Now.Year.ToString();
            cmbSSIDMonth.SelectedItem.Value = DateTime.Now.Month.ToString();
        }

        /// <summary>
        /// Fills the committees.
        /// </summary>
        private void FillCommittees()
        {
            strCommittees.DataSource = ViewModel.GetAllCommittees();
            strCommittees.DataBind();
        }

        ///// <summary>
        ///// Handles the GetReport event of the Master control.
        ///// </summary>
        ///// <param name="sender">The source of the event.</param>
        ///// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        //void Master_GetReport(object sender, EventArgs e)
        //{
        //    ReportDataSource datasource = new ReportDataSource("MonthlyAccountantReportDataSet",
        //          ViewModel.GetAccountantMonthlyData(int.Parse(cmbSSIDMonth.SelectedItem.Value), cmbCommittees.SelectedItem.Value.ToGuid(), int.Parse(txtYear.Text)).Tables[0]);

        //    ReportViewer1.LocalReport.DataSources.Clear();
        //    ReportViewer1.LocalReport.DataSources.Add(datasource);
        //    ReportViewer1.LocalReport.Refresh();

        //}
    }
}

